@extends('layouts.default')

@section('title', '班主任')

@push('scripts')
<script>
    var filters = {};
    var per_page = 10;
    var totalNum = 0;
    layui.use(['layer','form','table','laytpl','laydate','element','laypage'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;
        var materials_date = "{{request()->get('materials_date',0)}}";
        var title = "{{request()->get('title','')}}";
        
        renderSelect();

        laydate.render({
            elem: '#award-date'
            ,type: 'date'
            ,range: true
        });

        // 搜索查询
        form.on('submit(queryBtn)', function(data) {
            var param = data.field;
            filters = param
            getList(1, per_page);
        });
        // 重置
        form.on('submit(resetBtn)', function(data) {
            filters = {};
            $('#qryForm')[0].reset();
            getList(1, per_page)
        });
        // 获取页面数据
        function getList(page, limit) {
            if ($.isEmptyObject(filters)){
                let param = form.val("qryForm");
                filters = param;
            }
            
            var para = {
                "page": page,
                "search": filters,
                "limit": limit
            };

            let chief_value = '';
            if(title.length >0){
                if(title == '副班主任'){
                    chief_value = '161';
                }else{
                    chief_value = '160';
                }
            }         

            Object.assign(para, {"search": {'status':'PASS','materials_date': materials_date,chief_value:chief_value}});	
            getHeadmasterList(para, function(res){
                if(res.code === 1) {
                    if(totalNum != res.total) {
                        totalNum = res.total;
                        per_page = res.limit;
                        renderPageBar(page,per_page)
                    }

                    table.render({
                        data: res.data,
                        elem: '#tablelsit',
                        toolbar: '#toolbarHead', //开启头部工具栏，并为其绑定左侧模板
                        defaultToolbar: [],
                        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        limit: res.limit,
                        skin: 'nob',
                        cols: [[
                            {type: 'checkbox', fixed: 'left'},
                            {field: 'term_name', title: '学年学期', align: 'left', templet: function(d) {
                                return d.data_version_id ? d.term.title : '-';
                            }},
                            {field: 'class_name', title: '班级名称', align: 'center', templet: function(d) {
                                let gradeSet = d.grade_set.length > 0 ? d.grade_set[0].selectValue : '';
                                let classSet = d.class_set.length > 0 ? d.class_set[0].selectValue : '';
                                return gradeSet || classSet ? gradeSet + classSet : '-';
                            }},
                            {field: 'chief', title: '正副情况', align: 'center', templet: function(d) {
                                return d.chief_set.length > 0 ? d.chief_set[0].selectValue : '';
                            }},
                            {field: 'begin_date', title: '开始日期', align: 'center'},
                            {field: 'end_date', title: '结束日期', align: 'center'},
                            {title: '操作', width: '20%', align: 'center', toolbar: '#opDemo', fixed:'right'}
                        ]],
                        id:"tableIns",
                        done: function(res, curr, count) {
                            // setBtnPermission();
                            curr_page = page;
                        },
                        page: false,
                        loading: true
                    });
                } else {
                    layer.msg(res.msg, {time: 2000, icon: 5});
                }
            });
        }

        // 渲染页面
        function renderPageBar(page,limit) {
            laypage.render({
                elem: 'pageBar',
                count: totalNum, //数据总数，从服务端得到
                curr: page,
                first: '第一页',
                last: '最后一页',
                limit:limit,
                layout: ['limit','count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj, first) {
                    //首次不执行
                    if(!first) {
                        layer.closeAll();
                        getList(obj.curr,obj.limit)
                    }
                }
            });
        }

        //表格头工具栏事件
        table.on('toolbar(tablelsit)', function(obj){
            switch(obj.event){
                case 'add':
                    
                break;
            };
        });
        //监听行工具事件
        table.on('tool(tablelsit)', function(obj){
            var data = obj.data;

            if(obj.event === 'del'){
                layer.confirm('确定删除吗?', function(index){
                    delHeadmaster({id: data.id}, function(res) {
                        if (res.code == '9') {
                            layer.msg(res.msg, {icon: 5, shift: 6});
                        } else {
                            layer.msg('删除成功', {icon: 6,time: 1000},function(){
                                obj.del();
                                layer.close(index);
                            });
                        }
                    });
                });
            }
        });

        function renderSelect(){
            getTermData({},function(res){
                if(res.code == 1){
                    $('[data-role=termList]').empty().append('<option value="">请选择</option>');

                    for(var i in res.data){
                        $('[data-role=termList]').append('<option value="' + res.data[i].id + '">' + res.data[i].title + '</option>');
                    }

                    form.render('select'); 
                }
            });
            
            getList(1, per_page);
        }

        $('#click-checkbox').click(function(){
            var checkStatus = table.checkStatus('tableIns');
            window.parent.checkboxdata = checkStatus;     
        })

    });
</script>
@endpush

@section('content')
<style>
    
</style>



<div class="layui-card">
    <script type="text/html" id="toolbarHead">
        <div class="list-head">
            <h3>班主任列表</h3>
            <div class="layui-btn-container list-opt-btns">
                <a btnPermission="" class="layui-btn layui-btn-normal" href="{{route('headmaster.addView')}}" id="addBtn"> + 新增 </a>
            </div>
        </div>
    </script>
 
    <div class="layui-card-body">
        <table class="layui-hide" id="tablelsit" lay-filter="tablelsit" lay-data="{id: 'tablelsit'}"></table>
        <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>
    </div>

    <script type="text/html" id="opDemo">
        <a btnPermission="" class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" href="{{route('headmaster.infoView')}}?id=@{{d.id}}">查看</a>
    </script>
</div>
<button style="display:none" id="click-checkbox" ></button>
@endsection